﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CompoundAdaptService.cs" company="">
//   
// </copyright>
// <summary>
//   The compound adapt service.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Libium.Adapters
{
    using System;
    using System.Collections.Generic;

    /// <summary>
    ///   The compound adapt service.
    /// </summary>
    public class CompoundAdaptService : IAdaptService
    {
        #region Fields

        /// <summary>
        ///   The _adapt services.
        /// </summary>
        private readonly List<IAdaptService> _adaptServices = new List<IAdaptService>();

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The can adapt.
        /// </summary>
        /// <param name="type1">
        /// The type 1. 
        /// </param>
        /// <param name="type2">
        /// The type 2. 
        /// </param>
        /// <returns>
        /// The <see cref="bool"/> . 
        /// </returns>
        public bool CanAdapt(Type type1, Type type2)
        {
            foreach (var adaptService in this._adaptServices)
            {
                if (adaptService.CanAdapt(type1, type2))
                {
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// The get adapt types.
        /// </summary>
        /// <param name="type">
        /// The type. 
        /// </param>
        /// <returns>
        /// The <see cref="IEnumerable"/> . 
        /// </returns>
        public IEnumerable<Type> GetAdaptTypes(Type type)
        {
            List<Type> result = new List<Type>();
            foreach (var adaptService in this._adaptServices)
            {
                var adaptTypes = adaptService.GetAdaptTypes(type);
                foreach (var adaptType in adaptTypes)
                {
                    if (!result.Contains(adaptType))
                    {
                        result.Add(adaptType);
                    }
                }
            }

            return result;
        }

        /// <summary>
        /// The try adapt.
        /// </summary>
        /// <param name="obj1">
        /// The obj 1. 
        /// </param>
        /// <param name="type">
        /// The type. 
        /// </param>
        /// <param name="obj2">
        /// The obj 2. 
        /// </param>
        /// <returns>
        /// The <see cref="bool"/> . 
        /// </returns>
        public bool TryAdapt(object obj1, Type type, out object obj2)
        {
            obj2 = null;
            foreach (var adaptService in this._adaptServices)
            {
                if (adaptService.TryAdapt(obj1, type, out obj2))
                {
                    return true;
                }
            }

            return false;
        }

        #endregion
    }
}